<?php

namespace Drupal\shusy_product\Entity;

use Drupal\shusy_product_stock\Entity\ShusyProductStock;
use Drupal\shusy_product_weight\Entity\ShusyProductWeight;
use Entity;
use EntityListWrapper;

class ShusyProductStandard extends Entity {

  public $id;

  public $vid;

  public $uid;

  public $op_uid;

  public $created;

  public $changed;

  public $is_new_revision;

  public $product_id;

  public $sku;

  public $standard;

  public $amount;

  public $cost;

  public $status;

  public $sort = 0;

  function __construct($values = []) {
    $this->is_new_revision = TRUE;
    parent::__construct($values, 'shusy_product_standard');
  }

  public function save() {
    $this->changed = REQUEST_TIME;
    if (empty($this->created)) {
      $this->created = REQUEST_TIME;
    }

    $this->op_uid = $GLOBALS['user']->uid;
    if (empty($this->uid)) {
      $this->uid = $GLOBALS['user']->uid;
    }
    parent::save();
    if ($this->status == 0) {
      //下架规格 检查是否所有规格都下架了 如果所有规格都下架 自动下架产品
      $es = shusy_product_standard_load_multiple(FALSE, [
        'product_id' => $this->product_id,
        'status' => 1,
      ]);
      if (empty($es)) {
        $p = shusy_product_load($this->product_id);
        if ($p instanceof ShusyProduct) {
          $p->status = 0;
          $p->save();
        }
      }
    }
  }

  function getProductName() {
    $name = '';
    $product = shusy_product_load($this->product_id);
    if ($product instanceof ShusyProduct) {
      $name = "{$product->name} ({$this->standard})";
    }
    return $name;
  }

  function imagefid() {
    $wrapper = $this->wrapper();
    $field_image = $wrapper->field_image->value();
    if (!empty($field_image)) {
      return $field_image['fid'];
    }
    $product = shusy_product_load($this->product_id);
    if ($product instanceof ShusyProduct) {
      $productWrapper = $product->wrapper();
      $field_image = $productWrapper->field_image->value();
      if (empty($field_image)) {
        $field_images = $productWrapper->field_images;
        if ($field_images instanceof EntityListWrapper) {
          foreach ($field_images as $image) {
            $field_image = $image->value();
            break;
          }
        }
      }
    }
    return $field_image['fid'];
  }

  function stockChange(int $number) {
    if (!empty($this->id)) {
      $es = shusy_product_stock_load_multiple(FALSE, ['product_standard_id' => $this->id]);
      if (!empty($es)) {
        $e = reset($es);
        if ($e instanceof ShusyProductStock) {
          $e->stock += $number;
          $e->save();
        }
      }
    }
  }

  function stock() {
    $t = 0;
    if (empty($this->id)) {
      $es = shusy_product_stock_load_multiple(FALSE, ['product_standard_id' => $this->id]);
      if (!empty($es)) {
        $e = reset($es);
        if ($e instanceof ShusyProductStock) {
          $t = $e->stock;
        }
      }
    }
    return $t;
  }

  function weight() {
    $t = 0;
    if (!empty($this->id)) {
      $es = shusy_product_weight_load_multiple(FALSE, ['product_standard_id' => $this->id]);
      if (!empty($es)) {
        $e = reset($es);
        if ($e instanceof ShusyProductWeight) {
          //单位 0 克 1 千克 2 吨
          switch ($e->unit) {
            case 0:
              $t = $e->weight / 1000;
              break;
            case 1:
              $t = ceil($e->weight / 1000 * 1000);
              break;
            case 2:
              $t = ceil($e->weight / 1000 * 1000 * 1000);
              break;
          }
        }
      }
    }
    return $t;
  }
}
